package com.hdw.med.controller;

import com.hdw.common.base.BaseResult;
import com.hdw.common.base.mapper.BaseMapper;
import com.hdw.common.util.DateUtil;
import com.hdw.common.util.LocalDateUtil;
import com.hdw.common.util.SpringBeanUtil;
import com.hdw.common.vo.LayMsg;
import com.hdw.common.vo.PaginationVO;
import com.hdw.med.api.MdMedPubfldService;
import com.hdw.med.bean.model.BaseIndex;
import com.hdw.med.bean.model.MdMedPubfld;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.*;

@RestController
@RequestMapping("/etl/mdDim/")
@Api( tags="维度处理")
public class MdDimController {

    @Autowired
    BaseMapper baseMapper;

    @ApiOperation(value="插入天数据")
    @RequestMapping(value="insertDay", method= RequestMethod.GET)
    public void insertDay(String startDate,String endDate) {
        List<Map<String,Object>> list = new ArrayList<>();
        try {
            Date dtBegin = LocalDateUtil.dateFormat.parse(startDate+" 00:00:00");
            Date dtEnd = LocalDateUtil.dateFormat.parse(endDate+" 00:00:00"),dtNext;
            String dBegin;int counterStop=0;
            Map<String,Object> dayMap = new TreeMap<>();
            while (dtBegin.before(dtEnd) && counterStop++ < 10000) {
                dBegin = DateUtil.toDateStrByFormat(dtBegin, "yyyyMMdd");
                dayMap = new TreeMap<>();
                dayMap.put("cd",dBegin);dayMap.put("mm",dBegin.substring(0,6));dayMap.put("yy",dBegin.substring(0,4));
                dayMap.put("qq",dBegin.substring(0,4)+(Integer.parseInt(dBegin.substring(4,6))+2)/3);
                dayMap.put("ww",DateUtil.getWeek(dtBegin)+"");
                dayMap.put("na_ww",DateUtil.getWeekDes(dtBegin));
                list.add(dayMap);
                dtNext = DateUtil.getNextDate(dtBegin, 3);
                dtBegin = dtNext;
            }
            baseMapper.insertMapList("md_dim_day",list.get(0),list);
        }catch (Exception e) {
           e.printStackTrace();
        }
    }
    @Autowired
    MdMedPubfldService mdMedPubfldService;

    @ApiOperation(value="获取维度字典数据")
    @RequestMapping(value="findDimData", method= RequestMethod.GET)
    public BaseResult findDimData(String idPubfld) {
        String cdTb = mdMedPubfldService.getById(idPubfld).getCdTbDim();
        List<Map<String,Object>> list = null;
        if(StringUtils.isNotBlank(cdTb)) {
            String sql = "select * from "+cdTb;
            list = baseMapper.findBySql(sql);
        }
        return BaseResult.success(list,"");
    }

    public static void main(String argus[]) {
        try {
            System.out.println(DateUtil.getWeek(LocalDateUtil.dateFormat.parse("20220108 00:00:00")));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
